﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Reflection;
using MyCommunity.DataAccess;
using Autofac;
using MyCommunity.BusinessLogic;
using Autofac.Integration.Mvc;

namespace MyCommunity
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "EventIndex",
                "EventIndex/",
                new { controller = "Event", action = "Index" }
                );

            /*routes.MapRoute(
                "Details",
                "EventIndex",
                new { controller = "Event", action = "Index", query = UrlParameter.Optional }
                );*/


            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            this.AuthenticateRequest += new EventHandler(MvcApplication_AuthenticateRequest);

            //create IoC-container
            var builder = new ContainerBuilder();

            builder.RegisterControllers(Assembly.GetExecutingAssembly());

            builder.RegisterModule(new MyDALModule());
            builder.RegisterModule(new MyServicesModule());

            //set Autofac as default Dependency Resolver for application
            DependencyResolver.SetResolver(new AutofacDependencyResolver(builder.Build()));

            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }

        void MvcApplication_AuthenticateRequest(object sender, EventArgs e)
        {
            if (HttpContext.Current.Request.IsAuthenticated)
            {
                var mail = HttpContext.Current.User.Identity.Name; // E-Mail

                var user = new UserDAL().GetUser(mail);

                if (user != null)
                {
                    if (user.IsAdmin)
                    {
                        HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(new System.Security.Principal.GenericIdentity(mail), new string[] { RolesUtil.Admin });
                    }
                    else
                    {
                        HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(new System.Security.Principal.GenericIdentity(mail), new string[] { RolesUtil.User });
                    }
                }
            }
        }
    }

    public class MyDALModule : Autofac.Module
    {

        //override Load method
        protected override void Load(ContainerBuilder builder)
        {
            builder.Register(c => new UserDAL()).As<IUserDAL>();
            builder.Register(c => new UserEventAttendanceDAL()).As<IUserEventAttendanceDAL>();
        }
    }

    public class MyServicesModule : Autofac.Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder.Register(c => new UserService(c.Resolve<IUserDAL>())).As<IUserService>();
            builder.Register(c => new UserEventAttendanceService(c.Resolve<IUserEventAttendanceDAL>())).As<IUserEventAttendanceService>();
        }
    }

}